package fr.paulmasson.jfreex.channels;

import java.net.URL;

import javax.swing.ImageIcon;

import fr.paulmasson.jfreex.Channel;
import fr.paulmasson.jfreex.utils.JFreeXList;


public class LiveChannel implements Comparable<LiveChannel>, Channel {

	public enum Definition {
		HIGH, STANDARD, LOW
	}
	
	public String toStringDef(Definition def) {
		switch(def) {
		case HIGH:
			return "Haute définition";
		case STANDARD:
			return "Définition standard";
		case LOW:
			return "Basse définition";
		}
		return "nop";
	}
	
	public enum Bunch {
		TNT, CANALSAT, CINEMA, MUSIQUE, JEUNESSE, INFORMATION, SPORT, DOCUMENTAIRE, ETRANGERES, NONE
	}

	private int id;
	private String title;
	private int num;
	private int service;
	private Definition[] defs;
	
	private Definition current_def = Definition.HIGH;
	

	private Bunch bunch;
	

	
	public LiveChannel(int id, String title, int num, int service, Bunch bunch, Definition[] defs) {
		this.id = id;
		this.title = title;
		this.num = num;
		this.service = service;
		this.defs = defs;
		this.bunch = bunch;
		

		if (JFreeXList.contains(Definition.STANDARD, defs)) {
			current_def = Definition.STANDARD;
		} else if (JFreeXList.contains(Definition.LOW, defs)) {
			current_def = Definition.LOW;
		} else if (JFreeXList.contains(Definition.HIGH, defs)) {
			current_def = Definition.HIGH;
		}
		
	}
	
	public String getBunch() {
		switch(this.bunch) {
			case TNT:
				return "tnt";
			case CANALSAT:
				return "canalsat";
			case CINEMA:
				return "cinema";
			case MUSIQUE:
				return "musique";
			case JEUNESSE:
				return "jeunesse";
			case INFORMATION:
				return "information";
			case SPORT:
				return "sport";
			case DOCUMENTAIRE:
				return "documentaire";
			case ETRANGERES:
				return "etrangeres";
			case NONE:
				return "none";
			default:
				return "tnt";
		}
	}
	
	public Definition getCurrentDef() {
		return current_def;
	}
	
	public void setCurrentDef(Definition def) {
		this.current_def = def;
	}
	
	public String getIconName() {
		return this.title.replace(" ", "").toLowerCase() + ".png";
	}
	
	public String getIconNameReplay() {
		return this.title.replace(" ", "").toLowerCase() + "replay.png";
	}
	
	public String getIconPath() {
		URL url = this.getClass().getResource("/data/"+getIconName());
		if (url != null) {
			return url.toString().replace("file:","");
		} else {
			return this.getClass().getResource("/data/blank.png").toString().replace("file:","");
		}
	}
	
	
	
	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	

	public int getService() {
		return service;
	}

	public void setService(int service) {
		this.service = service;
	}

	public String getUrl(Definition def) {
		switch (def) {
		case HIGH:
			return "rtsp://mafreebox.freebox.fr/fbxtv_pub/stream?namespace=1&service="
					+ service + "&flavour=hd";
		case STANDARD:
			return "rtsp://mafreebox.freebox.fr/fbxtv_pub/stream?namespace=1&service="
					+ service + "&flavour=sd";
		case LOW:
			return "rtsp://mafreebox.freebox.fr/fbxtv_pub/stream?namespace=1&service="
					+ service + "&flavour=ld";
		}
		return "";
	}
	
	public String getUrl() {
		if (JFreeXList.contains(Definition.STANDARD, this.defs)) {
			return "rtsp://mafreebox.freebox.fr/fbxtv_pub/stream?namespace=1&service="
			+ service + "&flavour=sd";
		} else if (JFreeXList.contains(Definition.LOW, this.defs)) {
			return "rtsp://mafreebox.freebox.fr/fbxtv_pub/stream?namespace=1&service="
			+ service + "&flavour=ld";
		} else if (JFreeXList.contains(Definition.HIGH, this.defs)) {
			return "rtsp://mafreebox.freebox.fr/fbxtv_pub/stream?namespace=1&service="
			+ service + "&flavour=hd";
		} else {
			return "no_url";
		}
	}
	
	public String getDefaultDef() {
		if (JFreeXList.contains(Definition.STANDARD, this.defs)) {
			return toStringDef(Definition.STANDARD);
		} else if (JFreeXList.contains(Definition.LOW, this.defs)) {
			return toStringDef(Definition.LOW);
		} else if (JFreeXList.contains(Definition.HIGH, this.defs)) {
			return toStringDef(Definition.HIGH);
		} else {
			return "no_url";
		}
	}
	
	public Definition[] getDefs() {
		return this.defs;
	}
	
	public String toString() {
		return this.title;
	}


	@Override
	public int compareTo(LiveChannel o) {
		return new Integer(this.getId()).compareTo(o.getId());
	}

	public Integer getId() {
		return this.id;
	}

	@Override
	public ImageIcon getIcon() {
		// TODO Auto-generated method stub
		return null;
	}
}
